package com.itheima.test9;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author:gy
 * @Date: 2025/09/04 15:12
 * @Description:
 */
public class ClientDemo {

    public static void main(String[] args) throws Exception {
        //客户端  建立一个Socket
        Socket socket = new Socket("127.0.0.1", 54321);
        System.out.println("已连接到计算机服务");
        System.out.println("输入数学表达式(如:3 + 5 ),输入exit退出");
        //网络的输入输出流
        //字符缓冲输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //键盘录入
        Scanner sc = new Scanner(System.in);
        String s;
        while (true) {
            System.out.println(">");
            s = sc.nextLine();
            if ("exit".equals(s)) {
                System.out.println("连接已经关闭");
                bw.write(s);
                bw.newLine();
                socket.shutdownOutput();
                break;
            }
            bw.write(s);
            bw.newLine();
            bw.flush();
           // socket.shutdownOutput();
            //得到反馈
            String s1 = br.readLine();
            System.out.println("结果是"+s1);
        }
        socket.close();
    }
}
